﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using System.Collections.Generic;

public partial class System_DepartmentNew : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindParentList("");
            if (Request.QueryString["DepartmentID"] == null)
            {
                ActionTitle.Text = "添加单位";
            }
            else
            {
                ActionTitle.Text = "编辑单位";
                BindData();
                DepartmentID.ReadOnly = true;
            }
        }
    }
    private void BindParentList(string parentID)
    {
        ListItem li = new ListItem();
        if (parentID == "")
        {
            li.Text = "支队级单位";
            li.Value = "";
            ParentList.Items.Add(li);
        }
        List<DepartmentInfo> departments = Yipusoft.Account.BLL.Department.GetList("ParentID='" + parentID + "' ORDER BY DepartmentID");
        foreach (DepartmentInfo department in departments)
        {
            li = new ListItem();
            li.Text = department.FullName + "(" + department.DepartmentID + ")";
            li.Value = department.DepartmentID;
            ParentList.Items.Add(li);
            BindParentList(department.DepartmentID);
        }
    }
    private void BindData()
    {
        DepartmentInfo department = Yipusoft.Account.BLL.Department.GetDepartment(Request.QueryString["DepartmentID"]);
        DepartmentID.Text = department.DepartmentID;
        ParentList.SelectedIndex = ParentList.Items.IndexOf(ParentList.Items.FindByValue(department.ParentID));
        FullName.Text = department.FullName;
        ShortName.Text = department.ShortName;
        AliasName.Text = department.AliasName;
        StateList.SelectedIndex = StateList.Items.IndexOf(StateList.Items.FindByValue(department.State.ToString()));
    }
    private void Insert()
    {
        if (Yipusoft.Account.BLL.Department.Exsits(DepartmentID.Text))
        {
            Prompt.Text = "警告：该单位编码已存在！";
            return;
        }
        DepartmentInfo department = new DepartmentInfo();
        department.DepartmentID = DepartmentID.Text;
        department.ParentID = ParentList.SelectedValue.Trim();
        department.FullName = FullName.Text;
        department.ShortName = ShortName.Text;
        department.AliasName = AliasName.Text;
        department.State = int.Parse(StateList.SelectedValue);
        Yipusoft.Account.BLL.Department.Insert(department);
        Prompt.Text = "提示：添加成功！";
    }
    private void Update()
    {
        DepartmentInfo department = Yipusoft.Account.BLL.Department.GetDepartment(Request.QueryString["DepartmentID"]);
        department.ParentID = ParentList.SelectedValue.Trim();
        department.FullName = FullName.Text;
        department.ShortName = ShortName.Text;
        department.AliasName = AliasName.Text;
        department.State = int.Parse(StateList.SelectedValue);
        Yipusoft.Account.BLL.Department.Update(department);
        Prompt.Text = "提示：更新成功！";
    }
    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["DepartmentID"] == null)
            Insert();
        else
            Update();
    }
}
